/**
 * 二分算法
 */

//递归
var bitsection = (arr, num) => {
  function search(array, left, right) {
    if (left > right) return -1
    let mid = Math.floor((left + right) / 2)
    if (num > arr[mid]) {
      bitsection(array, mid + 1, right)
    } else if (num < arr[mid] && left < right) {
      bitsection(arr, left, mid - 1)
    } else {
      return mid
    }
  }
  return search(nums, 0, num.length - 1)
}

//二分
var search = function (nums, target) {
  let low = 0
  let high = nums.length - 1
  while (high >= low) {
    let mid = Math.floor((low + high) / 2)
    if (nums[mid] > target) {
      high = mid - 1
    } else if (nums[mid] < target) {
      low = mid + 1
    } else if (nums[mid] == target) {
      return mid
    }
  }
  return -1
}
